package com.oa.controller;

import com.oa.bean.PageInfo;
import com.oa.bean.Dept;
import com.oa.service.DeptService;
import com.oa.service.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//使用DeptController来接收和处理dept模块的所有请求操作
//目的：一个请求处理类映射多个uri
@WebServlet(urlPatterns = {"/insertDept","/deleteDept","/queryDept","/toUpdateDept","/updateDept"})
public class DeptServlet extends HttpServlet {

    //获取业务层接口的对象
    DeptService deptService = new DeptServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");//请求对象设置编码
        //获取请求uri
        String uri = request.getRequestURI();
        System.out.println(uri);
        //response.getWriter().append("DeptController--"+uri);
        if(uri.contains("queryDept")){
            queryDept(request,response);     //查询请求处理方法
        }else if(uri.contains("insertDept")){
            insertDept(request,response);   //添加请求处理方法
        }
        else if(uri.contains("toUpdateDept")){//跳转修改页面请求
            toUpdateDept(request,response); //修改页面请求
        }
        else if(uri.contains("updateDept")){
            updateDept(request,response); //修改请求
        }else if(uri.contains("deleteDept")){
            deleteDept(request,response); //删除请求
        }
    }
    
    private void insertDept(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //设置响应编码为gbk
        request.setCharacterEncoding("UTF-8");//请求对象设置编码
        //1、从请求对象中获取请求参数 通过getParameter方法根据key来获取value
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        System.out.println("name:"+name+",remark:"+remark);
        //创建用户将数据封装
        Dept dept = new Dept();
        dept.setName(name);dept.setRemark(remark);
        // 2、调用业务层实现功能
        boolean isok = deptService.insertDept(dept);
        //3、做出响应--跳转页面
        if(isok){//成功跳转首页
            request.getRequestDispatcher("queryDept").forward(request,response);
        }else{
            request.getRequestDispatcher("DeptInsert.jsp").forward(request,response);
        }
    }

    // 只能删除没有被其他表外键引用的
    private void deleteDept(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、从请求对象中获取请求参数 通过getParameter方法根据key来获取value
        String strId = request.getParameter("id");
        int id = Integer.parseInt(strId);
        System.out.println(id);
        // 2、调用业务层实现功能
        boolean isOk = true;
        try {
            isOk = deptService.deleteDept(id);
        }catch(Exception e){
            e.printStackTrace();
            isOk = false;
        }finally {
            request.setAttribute("info", "删除部门[id="+id+"]情况：" + (!isOk ? "失败" : "成功"));
            // 3、重新查询
            request.getRequestDispatcher("queryDept").forward(request, response);
        }
    }

    private void updateDept(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应编码为gbk
        request.setCharacterEncoding("UTF-8");//请求对象设置编码
        //1、从请求对象中获取请求参数 通过getParameter方法根据key来获取value
        String strId = request.getParameter("id");
        int id = Integer.parseInt(strId);
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        Dept dept = new Dept();
        dept.setId(id);dept.setName(name);dept.setRemark(remark);
        //创建用户将数据封装
        // 2、调用业务层实现功能
        boolean isok = deptService.updateDept(dept);
        // 3、做出响应--跳转页面
        if(isok){//成功重新查询
            request.getRequestDispatcher("queryDept").forward(request,response);
        }else{//失败跳转修改页面
            request.getRequestDispatcher("toUpdateDept?id"+id).forward(request,response);
        }
    }

    private void queryDept(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //设置响应编码为gbk
        // response.setCharacterEncoding("GBK");
        String pn = request.getParameter("pn");
        System.out.println("pn:"+pn);
        String size = request.getParameter("pageSize");
        System.out.println("pageSize:"+size);
        String oneDeptName = request.getParameter("name");
        System.out.println("matched name:"+oneDeptName);
        int totalDeptNum = 0;
        int pageNo = 1,pageSize = 4;
        List<Dept> deptList = null;
        PageInfo pageInfo = null;
        if(pn!=null&&!pn.equals("")){
            pageNo = Integer.parseInt(pn);
        }
        if(size!=null&&!size.equals("")){
            pageSize = Integer.parseInt(size);
        }

        // 1.调用业务层中的查询方法
        if(oneDeptName !=null&&! oneDeptName.equals("")){ // 模糊查询
            totalDeptNum = deptService.queryDeptByMatchNameCount(oneDeptName);
        }else {
            totalDeptNum = deptService.queryDeptCount();
        }

        if(totalDeptNum>0&&totalDeptNum<=pageSize){
            pageSize = totalDeptNum;
        }else if(totalDeptNum==0){
            pageSize=1;
        }

        if(oneDeptName !=null&&! oneDeptName.equals("")) { // 模糊查询
            pageInfo = new PageInfo(pageNo, totalDeptNum, pageSize);
            deptList = deptService.queryDeptByMatchName(pageInfo, oneDeptName);
        }else {
            pageInfo = new PageInfo(pageNo, totalDeptNum, pageSize);
            deptList = deptService.queryDept(pageInfo);
        }

        // 2.保存数据到作用域中
        request.setCharacterEncoding("UTF-8");//请求对象设置编码
        request.setAttribute("deptList", deptList);
        request.setAttribute("pageInfo", pageInfo);
        if(oneDeptName !=null&&! oneDeptName.equals(""))
            request.setAttribute("matchDeptName",oneDeptName);
        else
            request.setAttribute("matchDeptName","");
        //转发是请求链没有断开的
        request.getRequestDispatcher("DeptIndex.jsp").forward(request,response);
    }

    private void toUpdateDept(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取ID,根据id来查询
        String deptId = request.getParameter("id");
        int id = Integer.parseInt(deptId);
        Dept dept = deptService.queryDeptById(id);
        System.out.println(dept);
        //2.保存数据
        request.setAttribute("dept",dept);
        //3.跳转页面
        request.getRequestDispatcher("DeptUpdate.jsp").forward(request,response);
    }
}
